<?php
namespace app\common\model;
use think\Model;

class Users extends Model {
	protected $tableName = 'users';
	public function register(){
		$data['user_name'] = input('username');
		$data['password']  = input('password');
		$data['nickname']  = input('nickname');
		// 检查数据是不是完整
		$bool = checkrequestdate($data);
		if($bool['status'] == false){
			return $bool;
		}
		// 检查用户名是否存在
		$bool = $this->findByName($data['user_name']);
		if($bool['status'] == false){
			return $bool;
		}
		//修改密码
		$data['add_time'] = date('Y-m-d H:i:s');
		$data['salt'] = mt_rand(999,9999);
		$data['is_del'] = 0;
		$data['password'] = md5($data['password'].$data['salt']);
		$bool = Model($this->tableName)->save($data);
		if($bool){
			$out = array('status'=>'ok','msg'=>'注册成功');
		}else{
			$out = array('status'=>'false','msg'=>'数据库添加数据失败');
		}
		return $out;

		
	}
	//登陆
	public function login(){
		$data['user_name'] = input('username');
		$data['password']  = input('password');
		// 检查数据是不是完整
		$bool = checkrequestdate($data);
		if($bool['status'] == false){
			return $bool;
		}
		// 检查用户名是否存在
		$have = $this->findByName($data['user_name']);
		// dump($have);
		if($have['status'] == false){
			$users = $have['content'];
			// dump($users);//die();
			$password = md5($data['password'].$users['salt']);
			// dump($password);
			if($users['password'] != $password){
				$out = array('status'=>false,'msg'=>'密码输入不正确');
				return $out;
			}
			$where = array('id'=>$users['id']);
			$save = array('update_time'=>date('Y-m-d H:i:s'));
			// die('ok');
			$bool = Model($this->tableName)->where($where)->update($save);
			// dump($users);die();
			if($bool){
				$out = array('status'=>'ok','msg'=>'登陆成功','content'=>$users);
			}else{
				$out = array('status'=>'false','msg'=>'数据库修改数据失败');
			}
		}else{
			$out = array('status'=>false,'msg'=>'账号不存在');
		}
		
		return $out;
	}

	public function findByName($user_name){
		$where = array('user_name'=>$user_name);
		// dump($have);
		$have = $this->field('id,user_name,password,nickname,salt')->where($where)->find();
		// dump($have);
		if($have){
    		$content['id'] = $have['id'];
    		$content['user_name'] = $have['user_name'];
    		$content['password'] = $have['password'];
    		$content['salt'] = $have['salt'];
    		$content['nickname'] = $have['nickname'];
			return array('status'=>false,'msg'=>'the database had this user_name','content'=>$content);
		}else{
			return array('status'=>'ok','msg'=>'the user_name can be used');
		}
	}
	public function restPassword(){
		$username = input('username');
		$data['password']  = input('password');
		// 检查数据是不是完整
		$bool = checkrequestdate($data);
		if($bool['status'] == false){
			return $bool;
		}
		$data['salt'] = mt_rand(999,9999);
		$data['password'] = md5($data['password'].$data['salt']);
		$data['update_time'] = date('Y-m-d H:i:s');
		// 查找设备是否已经绑定了用户
		$bool = $this->save($data,['user_name'=>$username]);
		if($bool){
			return array('status'=>'ok','msg'=>'密码重置成功！');
		}else{
			$out = array('status'=>false,'msg'=>'密码重置失败！');
			return $out;
		}

		
	}

	
}